#爬取王者荣耀壁纸
import json
import urllib.request
from urllib.parse import unquote
def get_pixel_size(i):
    pix_size=["1024x768","1280x720","1280x1024","1440x900","1920x1080","1920x1200","1920x1440"]
    return pix_size[int(i)-2]

def dowload(page):
    url = 'https://apps.game.qq.com/cgi-bin/ams/module/ishow/V1.0/query/workList_inc.cgi?activityId=2735&sVerifyCode=ABCD&sDataType=JSON&iListNum=20&totalpage=0&page='+str(page)+'&iOrder=0&iSortNumClose=1&jsoncallback=jQuery17107807466425877212_1650630019647&iAMSActivityId=51991&_everyRead=true&iTypeId=2&iFlowId=267733&iActId=2735&iModuleId=2735&_=1650630330842'
    #模拟浏览器请求
    headers = {
        'user-agent': ' Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36'
    }
    proxies_poll = [
        {'http': '103.37.141.69:80'},
        {'http': '118.24.219.151:16817'}
    ]
    import random
    proxies = random.choice(proxies_poll)
    request = urllib.request.Request(url=url, headers=headers)
    handler = urllib.request.ProxyHandler(proxies=proxies)
    opener = urllib.request.build_opener(handler)
    response = opener.open(request)
    content = response.read().decode("utf8")

    #去掉外层括号
    content = content.split("(")[1].split(")")[0]
    #字符串转成json
    obj = json.loads(content)
    url_list = obj['List']
    print(url_list)
    for i in range(len(url_list)):

        #一个英雄有8张分辨率不同的url，所以在此循环8次
        for j in range(8):
            if((j+1)<8):
                url = unquote(obj['List'][i]['sProdImgNo_'+str(j+1)])
                new_url = url.replace("/200","/0")
                name = unquote(obj['List'][i]["sProdName"])
                print(new_url)
                type = int(url[url.rfind("_")+1])
                px_size=get_pixel_size(type)
                print(px_size)
                urllib.request.urlretrieve(new_url, "F:\\py_project\\py_spider\\files\\" + name+'('+px_size+')'+ ".jpg")

if __name__ == '__main__':

    input_page =input("请输入页数")
    page = int(input_page)-1
    dowload(page)
